<?php
/*
 * Author :nikos bikos
 * Improved by:pandiam
 */
session_start();

if ( isset($_SESSION['uid']) && $_SESSION['uid'] != -1) {
    header('Location: index.php');
}

require_once ('../BL/BL.php');
require_once ('include/functions.php');
require_once ('../Frameworks/Objects/user.php');
UI_initErrors();

if(empty($_POST['username']) || empty($_POST['password'])){
	UI_registerError('msg_error', "Your username or password is invalid");
	header( 'Location: login.php' ) ;
}
else if($_POST['username'] == "" || $_POST['password'] == ""){
	UI_registerError('msg_error', 'Your username or password is invalid');
	header( 'Location: login.php' ) ;
}
else if( ($uid = BL_userLogin($_POST['username'], $_POST['password']) ) >= 0){
	$_SESSION['uid'] = $uid;
	$_SESSION['username'] = $_POST['username'];
    $user = BL_getUserData($uid);
    if(strcmp($user->getUserType(),"adm")==0)
        $_SESSION["Admin"] = $uid;
	UI_registerError('msg_success', 'You have successfully logged-in');
	header( 'Location: index.php' ) ;
}
else {
	/////UI_registerError('msg_error', 'Your username or password is invalid');
	header( 'Location: login.php' ) ;
}

?>
